<?php
    class Sort extends MY_Controller {

        public function index()
        {
            $this->load->model('admin/Sort_model');
            $pid=0;
            $model=$this->Sort_model->get_sort_by_pid($pid);
            $treelist="";
            if ($model->num_rows() > 0)
            {

                foreach ($model->result() as $row)
                {

                    $treelist=$treelist."<table width='100%' border='0' cellspacing='0' cellpadding='2'>\r\n";
                    $treelist=$treelist. "  <tr>\r\n";
                    $treelist=$treelist. "  <td style='background-color:#FBFCE2;' width='2%' class='bline'><img style='cursor:pointer' id='img".$row->id."' onClick=\"LoadSuns('suns".$row->id."',$row->id);\" src='".base_url()."public/images/dedeexplode.gif' width='11' height='11'></td>\r\n";
                    $treelist=$treelist. "  <td style='background-color:#FBFCE2;' class='bline'><table width='98%' border='0' cellspacing='0' cellpadding='0'><tr><td width='50%'><a href='".site_url("admin/sort/sort_modify?id=".$row->id)."'>".$row->name."[ID:".$row->id."]</a>";
                    $treelist=$treelist. "    </td><td align='right'>";
                    $treelist=$treelist. "<a href='".site_url("admin/sort/sort_add?id=".$row->id)."'>增加子类</a>";
                    $treelist=$treelist. "|<a href='".site_url("admin/sort/sort_modify?id=".$row->id)."'>更改</a>";
                    $treelist=$treelist. "|<a  href='".site_url("admin/sort/sort_del?id=".$row->id)."'  onclick=\"return confirm('是否将此分类删除?')\">删除</a>";
                    $treelist=$treelist. "&nbsp;</td></tr></table></td></tr>\r\n";
                    $treelist=$treelist. "</td></tr>\r\n</table>\r\n";
                    $treelist=$treelist. "    <table width='100%' border='0' cellspacing='0' cellpadding='0'>\r\n";
                    $treelist=$treelist. "     <tr><td id='suns".$row->id."' style='display:block'><table width='100%' border='0' cellspacing='0' cellpadding='0'>\r\n";
                    $treelist=$treelist.$this->_ListAllSunType($row->id,"　");
                    $treelist=$treelist. "    </table></td></tr>\r\n";
                    $treelist=$treelist. "    </table>\r\n";


                }
            }
            $data['treelist']=$treelist;    
            $this->load->view('admin/sort_index',$data);
        }

        //获得子类目的递归调用
        function _ListAllSunType($id, $step)
        {
            $treelist="";
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_pid($id);

            if ($model->num_rows() > 0)
            {

                foreach ($model->result() as $row)
                {



                    $treelist=$treelist. "<tr height='24' oncontextmenu=\"CommonMenu(event,this,".$row->id.",'".urlencode($row->name)."')\">\r\n";
                    $treelist=$treelist. "<td class='nbline'>";
                    $treelist=$treelist. "<table width='98%' border='0' cellspacing='0' cellpadding='0'>";
                    $treelist=$treelist. "<tr onMouseMove=\"javascript:this.bgColor='#FAFCE0';\" onMouseOut=\"javascript:this.bgColor='#FFFFFF';\"><td width='50%'>";
                    $treelist=$treelist. $step." <img style='cursor:pointer' id='img".$row->id."' onClick=\"LoadSuns('suns".$row->id."',$row->id);\" src='".base_url()."public/images/dedeexplode.gif' width='11' height='11'> <a href='".site_url("admin/sort/sort_modify?id=".$row->id)."'>".$row->name."[ID:".$row->id."]</a>";
                    $treelist=$treelist. "</td><td align='right'>";
                    $treelist=$treelist. "<a href='".site_url("admin/sort/sort_add?id=".$row->id)."'>增加子类</a>";
                    $treelist=$treelist. "|<a href='".site_url("admin/sort/sort_modify?id=".$row->id)."'>更改</a>";
                    $treelist=$treelist. "|<a href='".site_url("admin/sort/sort_del?id=".$row->id)."'  onclick=\"return confirm('是否将此分类删除?')\">删除</a>";
                    $treelist=$treelist. "&nbsp; </td></tr></table></td></tr>\r\n";

                    $treelist=$treelist. "  <tr><td id='suns".$row->id."' style='display:block'><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                    $treelist=$treelist.$this->_ListAllSunType($row->id,$step."　");
                    $treelist=$treelist. "</table></td></tr>\r\n";

                }
            }
            return $treelist;


        }

        //添加分类
        public function sort_add()
        {
            //获取id，从数据库中获取分类详细信息
            $id = $this->input->get('id',true);
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_id_one($id);
            $data['model']=$model;
            $this->load->view('admin/sort_add',$data);

        }

        //修改分类
        public function sort_modify()
        {
            $id = $this->input->get('id',true);
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_id_one($id);
			 $p_model=$this->Sort_model->get_sort_by_id_one($model->pid);
            $treelist=$this->Sort_model->get_sort_all();
            $data['model']=$model;
			 $data['p_model']=$p_model;
            $data['treelist']=$treelist;
            $this->load->view('admin/sort_modify',$data);


        }


        //保存分类信息
        public function sort_save()
        {
            $this->load->helper('common_function');
            $dopost = $this->input->post('dopost',true); 

            $this->load->model('admin/Sort_model');
            if($dopost=="add")
            {
                $pid = $this->input->post('pid',true); 
                $sortname = $this->input->post('sortname',true); 
                $model=$this->Sort_model->get_sort_by_name_one($sortname);
                if($model)
                {
                    ShowMsg('分类名称已经存在!',-1,0,1000);
                    exit; 
                }
                else
                {
                    //获取父分类的path值
                    $p_sort=$this->Sort_model->get_sort_by_id_one($pid);
                    $path='';
                    if($p_sort)
                    {
                        $path=$p_sort->path.'-';
                    }
                    $data = array(
                        'name' => $sortname ,
                        'pid' => $pid,
                        'path' => $path.$pid
                    );
                    $this->Sort_model->get_sort_add($data);
                    ShowMsg('添加成功!', site_url("admin/sort"),0,1000);
                    exit; 
                }


            }
            if($dopost=="modify")
            {
                $pid = $this->input->post('pid',true); 
                $id= $this->input->post('id',true);
                $sortname = $this->input->post('sortname',true); 
                $p_sort=$this->Sort_model->get_sort_by_id_one($pid);
                $path='';
                if($p_sort)
                {
                    $path=$p_sort->path.'-';
                }
                $data=array(
                    'name'=>$sortname,
                    'pid' => $pid,
                    'path' => $path.$pid
                );
                $this->Sort_model->get_sort_modify($id,$data);
                ShowMsg('更改成功!', site_url("admin/sort"),0,1000);
                exit; 

            }



        }

        //删除分类
        public function sort_del()
        {
            $this->load->helper('common_function');
            $this->load->model('admin/Sort_model');
            $id= $this->input->get('id',true);
            $chid_id=$this->_sort_id_child_list($id);
            $list_id=explode(',',$chid_id);
            foreach($list_id as $item)
            {
                $this->Sort_model->get_sort_del($item);
            }
            $this->Sort_model->get_sort_del($id);
            ShowMsg('删除成功!',site_url("admin/sort"),0,1000);
            exit; 
        }


        //返回与某个目相关的下级目录的类目ID列表
        public function _sort_id_child_list($id)
        {
            $total_id='';
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_pid($id);
            if ($model->num_rows() > 0)
            {
                foreach($model->result() as $row)
                {
                    $total_id=$total_id.$row->id.','; 
                    $total_id=$total_id.$this->_sort_id_child_list($row->id);
                }
            }

            return $total_id;
        }

    }

